import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
import index from '../pages/index'
import login from '../pages/login.vue'
import register from '../pages/register.vue'
import goods from '../pages/goods.vue'
import userCenter from '../pages/userCenter.vue'
import basicInfo from '../pages/basicInfo.vue'
import safetyInfo from '../pages/safetyInfo'
import bankInfo from '../pages/bankInfo.vue'
import myOrder from '../pages/myOrder.vue'
import printer from '../pages/printer'
import review from '../pages/review.vue'
import rePwd from '../pages/resetPwd'
import orderInfo from '../pages/orderInfo.vue'
import mealSetting from '../pages/mealSetting.vue'
Vue.use(Router)

const router = new Router({
  routes: [
    {path:'/index',redirect:'/'},
    {
      path: '/',
      name: 'index',
      component: index,
      meta:{
        title:"首页",
        footer:true,
        back:false,
        isLogin:true
      }
    },
    {
      path:'/login',
      name:'login',
      component:login,
      meta:{
        title:"登录",
        footer:false,
        back:false,
        isLogin:false
      }
    },
    {
      path:'/register',
      name:'register',
      component:register,
      meta:{
        title:"商户注册",
        footer:false,
        back:true,
        isLogin:false
      }
    },
    {
      path:'/goods',
      name:'goods',
      component:goods,
      meta:{
        title:"我的商品",
        footer:true,
        back:false,
        isLogin:true
      }
    },
    {
      path:'/userCenter',
      name:'userCenter',
      component:userCenter,
      meta:{
        title:"商户中心",
        footer:true,
        back:false,
        isLogin:true
      }
    },
    {
      path:'/basicInfo',
      name:'basicInfo',
      component:basicInfo,
      meta:{
        title:"基本信息修改",
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/safetyInfo',
      name:'safetyInfo',
      component:safetyInfo,
      meta:{
        title:'食品安全档案',
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/bankInfo',
      name:'bankInfo',
      component:bankInfo,
      meta:{
        title:'銀行卡信息',
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/myOrder',
      name:'myOrder',
      component:myOrder,
      meta:{
        title:'我的订单',
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/printer',
      name:'printer',
      component:printer,
      meta:{
        title:'我的打印机',
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/review',
      name:'review',
      component:review,
      meta:{
        title:'我的评价',
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/rePwd',
      name:'rePwd',
      component:rePwd,
      meta:{
        title:'忘记密码',
        footer:false,
        back:true,
        isLogin:false
      }
    },
    {
      path: '/orderInfo',
      name:'orderInfo',
      component:orderInfo,
      meta:{
        title:'订单详情',
        footer:false,
        back:true,
        isLogin:true
      }
    },
    {
      path:'/mealSetting',
      name:'mealSetting',
      component:mealSetting,
      meta:{
        title:'套餐设置',
        footer:false,
        back:true,
        isLogin:true
      }
    }
  ]
})
router.beforeEach((to, from, next) => {
  // to and from are both route objects. must call `next`.
  console.log(store.getters.token);
  if(to.meta.isLogin){
    if(store.getters.token){
      next();
    }else{
      next({path:"/login"})
    }
  }else{
    next();
  }
})
export default router;
